package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.bean.PersonClassBean;
import com.wnhz.work.yz.bean.PersonDistrictBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ------基本信息
 */
public class BasicMessageActivity extends BaseActivity implements View.OnClickListener {
    private final static int CLIENT_CODE = 689;

    private EditText et_basic_projectName;
    private EditText et_basic_person;
    private EditText et_basic_company;
    private TextView tv_basic_area;

    private String inputName, inputPerson, inputCompany, inputArea, inputType;

    private TextView tv_type;
    private LinearLayout ll_other_type;
    private EditText et_other_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_message);
        initData();
        initTitle();
        getPersonDistrict();
    }

    private ApplyMessagebean.BasicInfoEntity basicInfoEntity;

    private void initData() {
        basicInfoEntity = ApplyMessageActivity.bean.getBasic_info();
    }

    private List<PersonDistrictBean.InfoEntity> info1 = new ArrayList<>();

    private void getPersonDistrict() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_PERSONDISTRICT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        PersonDistrictBean bean = gson.fromJson(result, PersonDistrictBean.class);
                        info1 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                getPersonClass();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private List<PersonClassBean.InfoEntity> info2 = new ArrayList<>();

    private void getPersonClass() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_PERSONCLASS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        PersonClassBean bean = gson.fromJson(result, PersonClassBean.class);
                        info2 = bean.getInfo();
                        findViewById(R.id.submit).setOnClickListener(BasicMessageActivity.this);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();

                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }


    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("基本信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setText("保存");
        tv_right.setTextColor(Color.parseColor("#008c99"));
        tv_right.setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);

    }


    private void initView() {
        tv_basic_area = (TextView) findViewById(R.id.tv_basic_area);
        et_basic_projectName = (EditText) findViewById(R.id.et_basic_projectName);
        et_basic_person = (EditText) findViewById(R.id.et_basic_person);
        et_basic_company = (EditText) findViewById(R.id.et_basic_company);

        tv_type = (TextView) findViewById(R.id.tv_type);
        ll_other_type = (LinearLayout) findViewById(R.id.ll_other_type);
        et_other_type = (EditText) findViewById(R.id.et_other_type);


        if (basicInfoEntity.getProject_name() != null && !
                basicInfoEntity.getProject_name().equals("")) {
            tv_basic_area.setText(basicInfoEntity.getDistrict());
            et_basic_projectName.setText(basicInfoEntity.getProject_name());
            et_basic_person.setText(basicInfoEntity.getProject_person());
            et_basic_company.setText(basicInfoEntity.getVisit_company());
            tv_type.setText(basicInfoEntity.getPerson_class());
        }

        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {

            et_basic_projectName.setEnabled(false);
            et_basic_company.setEnabled(false);
            et_basic_person.setEnabled(false);
        } else {
            findViewById(R.id.rl_basic_area).setOnClickListener(this);
            findViewById(R.id.rl_type).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {

            case R.id.btn_main_left:

                finish();
                break;
            //保存
            case R.id.submit:

                inputName = et_basic_projectName.getText().toString().trim();
                inputPerson = et_basic_person.getText().toString().trim();
                inputCompany = et_basic_company.getText().toString().trim();
                inputType = tv_type.getText().toString().trim();
                inputArea = tv_basic_area.getText().toString().trim();
                checkInputMessage();

                break;

            //选择客户区域
            case R.id.rl_basic_area: {
                // MyToast("选择客户区域");
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info1.size(); i++) {
                    str.add(info1.get(i).getDistrict());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_basic_area.setText(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();

            }
            break;

            //客户类别
            case R.id.rl_type:
//                intent = new Intent(this, ClientTypeActivity.class);
//                startActivityForResult(intent, CLIENT_CODE);
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info2.size(); i++) {
                    str.add(info2.get(i).getPerson_class());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_type.setText(str.get(options1));
                        basicInfoEntity.setPerson_class(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();

                break;

            default:
                break;
        }
    }

    private void checkInputMessage() {
        if (!TextUtils.isEmpty(inputName) && !TextUtils.isEmpty(inputPerson) &&
                !TextUtils.isEmpty(inputCompany) && !TextUtils.isEmpty(inputArea)) {
            //TODO  保存操作
            basicInfoEntity.setPerson_class(inputType);
            basicInfoEntity.setDistrict(inputArea);
            basicInfoEntity.setProject_name(inputName);
            basicInfoEntity.setProject_person(inputPerson);
            basicInfoEntity.setVisit_company(inputCompany);
            ApplyMessageActivity.bean.setBasic_info(basicInfoEntity);
            setResult(RESULT_OK);
            finish();

        } else if (TextUtils.isEmpty(inputName)) {
            MyToast("请输入项目名称");
        } else if (TextUtils.isEmpty(inputPerson)) {
            MyToast("请输入负责人姓名");
        } else if (TextUtils.isEmpty(inputCompany)) {
            MyToast("请输入公司名称");
        } else if (TextUtils.isEmpty(inputArea)) {
            MyToast("请选择客户区域");
        } else if (TextUtils.isEmpty(inputType)) {
            MyToast("请输入类别");
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        switch (requestCode) {
//            case CLIENT_CODE:
//                if (resultCode == RESULT_OK) {
//                    String type = data.getStringExtra("client");
//                    tv_type.setText(type);
//                    String client = tv_type.getText().toString();
//                    if ("其他".equals(client)) {
//                        ll_other_type.setVisibility(View.VISIBLE);
//                    } else {
//                        ll_other_type.setVisibility(View.GONE);
//                    }
//                }
//                break;
//
//            default:
//                break;
//        }
//    }
}
